
#ifndef FILEOP_H_INCLUDED
#define FILEOP_H_INCLUDED
typedef struct data_file
{
		int **data;
		int height;
		int width;

} datafile_i2D;

typedef struct data_file_f
{
		float **data;
		int height;
		int width;

} datafile_f2D;
void write_file_int2D(char *filename, int **data, int height, int width)
{
        int row, col;
        FILE *fp;
        fp=fopen(filename,"w");
        fprintf(fp,"%d\t%d\n",height, width);
        for(row=0;row<height; row++)
        {
            for(col=0;col<width;col++)
            {
            	fprintf(fp,"%d\t",data[row][col]);
            }
            fprintf(fp,"\n");
        }
        fclose(fp);

}
void write_file_float2D(char *filename, float **data, int height, int width)
{
        int row, col;
        FILE *fp;
        fp=fopen(filename,"w");
        fprintf(fp,"%d\t%d\n",height, width);
        for(row=0;row<height; row++)
        {
            for(col=0;col<width;col++)
            {
            	fprintf(fp,"%f\t",data[row][col]);
            }
            fprintf(fp,"\n");
        }
    fclose(fp);
}

void write_file_int1D(char *filename, int *data, int height)
{
        int row;
        FILE *fp;

        fp=fopen(filename,"w");
        fprintf(fp,"%d\n",height);
        for(row=0;row<height; row++)
        {

            	fprintf(fp,"%d\t",data[row]);


        }
		fprintf(fp,"\n");
		    fclose(fp);

}
void write_file_float1D(char *filename, float *data, int height)
{
        int row;
        FILE *fp;

        fp=fopen(filename,"w");
        fprintf(fp,"%d\n",height);
        for(row=0;row<height; row++)
        {

            	fprintf(fp,"%f\t",data[row]);


        }
		fprintf(fp,"\n");
		    fclose(fp);

}
datafile_i2D read_file_int2D(char *filename)
{
	datafile_i2D d;
	int row, col, **data_i, height, width,dummy_val;
	FILE *fp;
	fp=fopen(filename,"rb");
	if(fp==NULL)
	{
		printf("\n----Invalid filename------\n");
		exit(EXIT_FAILURE);
	}

    dummy_val=fscanf(fp,"%d\t%d\n",&height, &width);

    data_i=alloc2Dint(height, width);
    for(row=0;row<height;row++)
    {
			for(col=0;col<width;col++)
			{
					dummy_val=fscanf(fp,"%d",&data_i[row][col]);
					//printf("%d\t",data_i[row][col])
			}
			//printf("\n");
    }
	d.data=data_i;
	d.height=height;
	d.width=width;
	    fclose(fp);

 return d;
}

datafile_f2D read_file_float2D(char *filename)
{
	datafile_f2D d;
	int row, col, height, width,dummy_val;
	float **data_f;
	FILE *fp;
	fp=fopen(filename,"r");
	if(fp==NULL)
	{
		printf("\n----Invalid filename------\n");
		exit(EXIT_FAILURE);
	}
	fseek(fp, 0,SEEK_SET);
    dummy_val=fscanf(fp,"%d\t%d",&height, &width);
    data_f=alloc2Dfloat(height, width);
    for(row=0;row<height;row++)
    {
			for(col=0;col<width;col++)
			{
					dummy_val=fscanf(fp,"%f",& data_f[row][col]);
			}
    }
	d.data=data_f;
	d.height=height;
	d.width=width;
    fclose(fp);

 return d;
}
/*void write_rule(char *f_name,r_set *r)
{
    FILE *fp;
    int row;
    ruleitem *r_item;
    fp=fopen(f_name,"w");
    for(row=0;row<no_of_rules;row++)
    {
        r_item=r[row].ptr;
        while(r_item!=NULL)
        {
                fprintf(fp,"%d:%f\t", r_item->attrib,r_item->val);
                r_item=r_item->nxtnode;
        }
        fprintf(fp,"\n");
    }
    fclose(fp);
}
float **merge_feature(float **vec1,float **vec2,int h,int w1,int w2)
{
    int row,col;
    float **tot_data;
    tot_data=alloc2Dfloat(h,(w1+w2));
    for(row=0;row<h;row++)
    {
        for(col=0;col<w1;col++)
        {
            tot_data[row][col]=vec1[row][col];
        }
        for(col=w1;col<w1+w2;col++)
        {
            tot_data[row][col]=vec2[row][col-w1];
        }
    }
    return tot_data;
}
float **merge_feature_class(float **hist, float *classval, int height,int width)
{
	int  row, col;
	float **dataset;
	dataset=alloc2Dfloat(height,width+1);
	for(row=0;row<height;row++)
	{
			for(col=0;col<width;col++)
			{
					dataset[row][col]=hist[row][col];
			}
			dataset[row][width]=classval[row];
	}
return dataset;

}

char * getfilename(char *filename, int ind)
{
    char *s, *c;
           s=alloc1Dchar(10);
           c=alloc1Dchar(256);
           intoa(ind, s);
           c=strcpy(c, filename);
           c=strcat(c, s);
           c=strcat(c, ".pgm");
   return c;
}*/
#else
#endif

